<template>
  <a-modal
    :title="title"
    v-model="show"
    :maskClosable="false"
    :width="1050"
    placement="right"
    :closable="true"
    @cancel="onClose"
    :footer="null"
    :visiable="clearPlanBasicVisiable"
  >
    <a-form :form="form">
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="清分次序"
                       v-bind="formItemLayout">
            <a-input v-decorator="['clearSeq',{rules: [{ required: true, message: '必填' }]}]" :disabled="true"/>
          </a-form-item>
          <a-form-item label="id"
                       v-bind="formItemLayout" style="display: none">
            <a-input v-decorator="['id']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="清算资金类型"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              v-decorator="['settleFundType',{rules: [{ required: true, message: '必填' }]}]">
              <a-select-option key="00">交易金额</a-select-option>
              <a-select-option key="01">商户手续费</a-select-option>
              <a-select-option key="02">商户入账净额</a-select-option>
              <a-select-option key="03">商户优惠手续费</a-select-option>
              <a-select-option key="04">代理商手续费</a-select-option>
              <a-select-option key="10">银联转接清算费</a-select-option>
              <a-select-option key="11">银联交换费(卡组织手续费）</a-select-option>
              <a-select-option key="12">品牌服务费</a-select-option>
              <a-select-option key="13">差错费用</a-select-option>
              <a-select-option key="14">支付通道手续费</a-select-option>
              <a-select-option key="20">收单手续费</a-select-option>
              <a-select-option key="21">发卡手续费</a-select-option>
              <a-select-option key="22">分行补贴手续费</a-select-option>
              <a-select-option key="30">跨行持卡人手续费</a-select-option>
              <a-select-option key="31">持卡人异地手续费</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="清分规则标识"
                       v-bind="formItemLayout">
            <a-select
              show-search
              placeholder="请输入代码或名称"
              option-filter-prop="children"
              style="width: 334px"
              :filter-option="filterOption"
              @focus="selectClearRule"
              @search="selectClearRule"
              v-decorator="['clearRuleId',{rules: [{ required: true, message: '必填字段'}]}]"
            >
              <a-select-option v-for="(d,index) in clearRuleData" :key="d.id">
                {{ d.id }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="清算规则标识"
                       v-bind="formItemLayout">
            <a-select
              show-search
              placeholder="请输入代码或名称"
              option-filter-prop="children"
              style="width: 334px"
              :filter-option="filterOption"
              @focus="selectSettleRule"
              @search="selectSettleRule"
              v-decorator="['settleRuleId',{rules: [{ required: true, message: '必填字段'}]}]"
            >
              <a-select-option v-for="(d,index) in settleRuleData" :key="d.id">
                {{ d.id }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <br>

    </a-form>
    <div class="drawer-bootom-button">
      <a-button @click="onClose" type="primary">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-modal>
</template>
<script>
  const formItemLayout = {
    labelCol: {span: 8},
    wrapperCol: {span: 16}
  }
  export default {
    name: 'ClearPlanBasic',
    props: {
      clearPlanBasicVisiable: {
        default: false
      },
      instId: '',
    },
    data() {
      return {
        title: '方案新增',
        loading: false,
        formItemLayout,
        form: this.$form.createForm(this),
        validateStatus: '',
        clearRuleData: [],
        settleRuleData: [],
        optFlagTmp: '',
        info: {}
      }
    },
    computed: {
      show: {
        get: function () {
          return this.clearPlanBasicVisiable
        },
        set: function () {
        }
      }
    },
    mounted() {

    },

    methods: {
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },

      selectClearRule(val) {
        this.clearRuleData = []
        let param = {}
        if (val) {
          param = {
            q: encodeURIComponent(val),
            pageSize: 20
          }
        } else {
          param = {
            q: '',
            pageSize: 20
          }
        }
        this.$get('admin/csmc/clearRule/clearRuleDialog', param).then((r) => {
          let result = r.data.results
          result.forEach(r => {
            this.clearRuleData.push(r);
          })
        })
      },
      selectSettleRule(val) {
        this.settleRuleData = []
        let param = {}
        if (val) {
          param = {
            q: encodeURIComponent(val),
            pageSize: 20
          }
        } else {
          param = {
            q: '',
            pageSize: 20
          }
        }
        this.$get('admin/csmc/settleRule/select2', param).then((r) => {
          let result = r.data.results
          result.forEach(r => {
            this.settleRuleData.push(r);
          })
        })
      },


      reset() {
        this.loading = false
        this.form.resetFields()
      },
      onClose() {
        this.reset()
        this.$emit('close')
      },
      handleSubmit() {
        this.form.validateFields((err, values) => {
          if (!err) {
            values.optFlag = this.optFlagTmp
            this.form.resetFields()
            this.$emit('success', values);
          }
        })
      },

      setClearPlanBasicValues({...info}) {
        let fields = ['id', 'clearSeq', 'settleFundType', 'clearRuleId', 'settleRuleId']
        this.optFlagTmp = info.optFlag
        if (info.optFlag == 'upd') {
          this.title = '方案修改'
        }
        Object.keys(info).forEach((key) => {
          if (fields.indexOf(key) !== -1) {
            this.form.getFieldDecorator(key)
            let obj = {}
            obj[key] = info[key]
            this.form.setFieldsValue(obj)
          }
        })
      },

    }

  }
</script>





